from selenium import webdriver
from selenium.webdriver import Keys
import pickle
import os
import time

# Настройка параметров ChromeOptions
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
options.add_argument(
    "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

driver.get("https://www.amazon.com")
time.sleep(3)

SEARCH_LOCATOR = ("xpath", "//input[@id='twotabsearchtextbox']")
ADD_CART_LOCATOR = ("xpath", "//button[@id='a-autoid-1-announce']")

search_field = driver.find_element(*SEARCH_LOCATOR)
search_field.click()
search_field.clear()
search_field.send_keys(
    "Logitech Signature M650 L Full Size Wireless Mouse - For Large Sized Hands, 2-Year Battery, Silent Clicks, Customizable Side Buttons, Bluetooth, for PC/Mac/Multi-Device/Chromebook - Black")
search_field.send_keys(Keys.ENTER)
time.sleep(2)
cart = driver.find_element(*ADD_CART_LOCATOR)
cart.click()
time.sleep(3)

pickle.dump(driver.get_cookies(), open(os.path.join(os.getcwd(), "cookies", "cookies.pkl"), "wb"))
driver.delete_all_cookies()
driver.refresh()
time.sleep(2)

pickle.load(open(os.path.join(os.getcwd(), "cookies", "cookies.pkl"), "rb"))
driver.refresh()

time.sleep(2)
